package com.shijun.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.shijun.bean.CommentBean;
import com.shijun.bean.DataBean;
import com.shijun.bean.FriendBean;
import com.shijun.config.TableShowImageConfig;
import com.shijun.customview.CustomAutoLineFeedView;
import com.shijun.customview.CustomListShowView;
import com.shijun.customview.CustomTableShowImageView;
import com.shijun.pullrecyclerviewdemo.R;
import com.shijun.utils.Utils;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhushijun on 2017/12/30 0030.
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private Context mContext;
    private List<DataBean> data;
    private LayoutInflater layoutInflater;
    private Map<Integer, SoftReference<CustomTableShowImageView>> tableShowImageViewCache = new HashMap<Integer, SoftReference<CustomTableShowImageView>>();
    private Map<Integer, SoftReference<CustomAutoLineFeedView>> autoLineFeedViewCache = new HashMap<Integer, SoftReference<CustomAutoLineFeedView>>();
    private Map<Integer, SoftReference<CustomListShowView>> listShowViewCache = new HashMap<Integer, SoftReference<CustomListShowView>>();
    private int imageAreaRootViewWidth;
    private OnListener listener;

    public MyAdapter(Context context, List<DataBean> data, int width) {
        layoutInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.data = data;
        this.imageAreaRootViewWidth = width;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.inflater_main_content_item, parent, false);
        MyViewHolder holder = new MyAdapter.MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final DataBean dataItem = data.get(position);

        Glide.with(mContext)
                .load(dataItem.getAvatar())
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.mipmap.default_avatar)
                .into(holder.avatarView);

        holder.nameView.setText(dataItem.getName());
        holder.contentView.setText(dataItem.getContent());

        holder.imgsView.removeAllViews();
        SoftReference<CustomTableShowImageView> tableShowImageView = tableShowImageViewCache.get(position);
        if (tableShowImageView == null || tableShowImageView.get() == null || tableShowImageView.get().getParent() != null) {
            tableShowImageView = new SoftReference<CustomTableShowImageView>(new CustomTableShowImageView(mContext, dataItem.getImgs(), TableShowImageConfig.STYPE_1, imageAreaRootViewWidth));
            tableShowImageViewCache.put(position, tableShowImageView);

        }
        holder.imgsView.addView(tableShowImageView.get());

        //--------------------------------------------------------------------------------------------------------
        holder.likeView.removeAllViews();
        SoftReference<CustomAutoLineFeedView> customAutoLineFeedView = autoLineFeedViewCache.get(position);
        if (customAutoLineFeedView == null || customAutoLineFeedView.get() == null || customAutoLineFeedView.get().getParent() != null) {
            customAutoLineFeedView = new SoftReference<CustomAutoLineFeedView>(new CustomAutoLineFeedView(mContext,dataItem.getLiskes(), imageAreaRootViewWidth));
            autoLineFeedViewCache.put(position, customAutoLineFeedView);
            customAutoLineFeedView.get().setOnClickItemListener(new CustomAutoLineFeedView.OnClickItemListener() {
                @Override
                public void onClickItem(View view, int position) {
                    Toast.makeText(mContext, "position:"+position+" name:"+((TextView)view).getText(), Toast.LENGTH_SHORT).show();
                }
            });
        }
        holder.likeView.addView(customAutoLineFeedView.get());

        //--------------------------------------------------------------------------------------------------------
        holder.commentView.removeAllViews();
        SoftReference<CustomListShowView> customListShowView = listShowViewCache.get(position);
        if (customListShowView == null || customListShowView.get() == null || customListShowView.get().getParent() != null) {
            customListShowView = new SoftReference<CustomListShowView>(new CustomListShowView(mContext, Utils.getCommentData()));
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            customAutoLineFeedView.get().setLayoutParams(params);
            customListShowView.get().setOnClickItemListener(new CustomListShowView.OnClickItemListener() {
                @Override
                public void onClickItem(TextView view, int postion) {
                    Toast.makeText(mContext, "position:"+position+" name:"+((TextView)view).getText(), Toast.LENGTH_SHORT).show();
                }
            });
        }
        holder.commentView.addView(customListShowView.get());
        //--------------------------------------------------------------------------------------------------------

        holder.likeButtonView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(dataItem.getLike()) {
                    listener.onLikeClick(false,position,holder);
                }else{
                    listener.onLikeClick(true,position,holder);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void  refreshCustomAutoLineFeedView(int position,MyViewHolder holder){
        DataBean dataBean = data.get(position);
//        SoftReference<CustomAutoLineFeedView> customAutoLineFeedView = autoLineFeedViewCache.get(position);
//        if(customAutoLineFeedView!=null&&customAutoLineFeedView.get()!=null) {
//            Log.i("TAG","getLiskes:"+dataBean.getLiskes());
//            customAutoLineFeedView.get().notifyDataSetChanged(dataBean.getLiskes());
//        }
        holder.likeView.removeAllViews();
        SoftReference<CustomAutoLineFeedView> customAutoLineFeedView = autoLineFeedViewCache.get(position);
        if (customAutoLineFeedView == null || customAutoLineFeedView.get() == null || customAutoLineFeedView.get().getParent() != null) {
            customAutoLineFeedView = new SoftReference<CustomAutoLineFeedView>(new CustomAutoLineFeedView(mContext,dataBean.getLiskes(), imageAreaRootViewWidth));
            autoLineFeedViewCache.put(position, customAutoLineFeedView);
            customAutoLineFeedView.get().setOnClickItemListener(new CustomAutoLineFeedView.OnClickItemListener() {
                @Override
                public void onClickItem(View view, int position) {
                    Toast.makeText(mContext, "position:"+position+" name:"+((TextView)view).getText(), Toast.LENGTH_SHORT).show();
                }
            });
        }
        holder.likeView.addView(customAutoLineFeedView.get());
    }

    public void  refreshCustomListShowView(int position,MyViewHolder holder){
        DataBean dataBean = data.get(position);
        SoftReference<CustomListShowView> customListShowView = listShowViewCache.get(position);
        if(customListShowView!=null&&customListShowView.get()!=null) {
            customListShowView.get().notifyDataSetChanged(dataBean.getComments());
        }
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {

        public ImageView avatarView;
        public TextView nameView;
        public LinearLayout imgsView;
        public TextView contentView;
        public LinearLayout likeView;
        public LinearLayout commentView;
        public Button likeButtonView;

        public MyViewHolder(View itemView) {
            super(itemView);
            nameView = (TextView) itemView.findViewById(R.id.inflater_main_content_item_name_tv);
            avatarView = (ImageView) itemView.findViewById(R.id.inflater_main_content_item_avatar_iv);
            contentView = (TextView) itemView.findViewById(R.id.inflater_main_content_item_content_tv);
            imgsView = (LinearLayout) itemView.findViewById(R.id.inflater_main_content_item_imgs_ll);
            likeView = (LinearLayout) itemView.findViewById(R.id.inflater_main_content_item_like_ll);
            commentView = (LinearLayout) itemView.findViewById(R.id.inflater_main_content_item_comment_ll);
            likeButtonView = (Button) itemView.findViewById(R.id.inflater_main_content_item_like_but);
        }
    }


    public void setOnListener(OnListener listener) {
        this.listener = listener;
    }

    public interface OnListener {
        void onCustomAutoLineFeedViewItemClick(TextView view, int postion,MyViewHolder holder);
        void onCustomListShowViewItemClick(TextView view, int postion);
        void onLikeClick(boolean isLike, int postion,MyViewHolder holder);
    }
}
